package org.dromara.oce.controller;

import java.util.List;

import cn.dev33.satoken.annotation.SaIgnore;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.oce.domain.bo.OceWrapTitleBo;
import org.dromara.oce.domain.vo.OceTitleVo;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.oce.domain.qo.OceTitleWrapQo;
import org.dromara.oce.domain.vo.OceTitleWrapVo;
import org.dromara.oce.domain.bo.OceTitleWrapBo;
import org.dromara.oce.service.OceTitleWrapService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 标题包
 *
 * @author dsbb
 * @date 2025-06-16
 * @module 管理平台接口
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/oce/titleWrap")
@SaIgnore
public class OceTitleWrapController extends BaseController {

    private final OceTitleWrapService oceTitleWrapService;

    /**
     * 查询标题包列表
     */
    @SaCheckPermission("oce:titleWrap:list")
    @GetMapping("/list")
    public TableDataInfo<OceTitleWrapVo> list(OceTitleWrapQo qo, PageQuery pageQuery) {
        return oceTitleWrapService.queryPageList(qo, pageQuery);
    }

    /**
     * 根据标题包查询下标题列表
     */
    @SaCheckPermission("oce:titleWrap:list")
    @GetMapping("/listTitle")
    public R<List<OceTitleVo>> listTitle(Long id) {
        return R.ok(oceTitleWrapService.queryTitleList(id));
    }

    /**
     * 导出标题包列表
     */
    @SaCheckPermission("oce:titleWrap:export")
    @Log(title = "标题包", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(OceTitleWrapQo qo, HttpServletResponse response) {
        List<OceTitleWrapVo> list = oceTitleWrapService.queryList(qo);
        ExcelUtil.exportExcel(list, "标题包", OceTitleWrapVo.class, response);
    }

    /**
     * 获取标题包详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("oce:titleWrap:query")
    @GetMapping("/{id}")
    public R<OceTitleWrapVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(oceTitleWrapService.queryById(id));
    }

    /**
     * 新增标题包
     */
    @SaCheckPermission("oce:titleWrap:add")
    @Log(title = "标题包", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody OceTitleWrapBo bo) {
        bo.checkParam();
        return toAjax(oceTitleWrapService.insertByBo(bo));
    }

    /**
     * 新增标题包下-标题
     */
    @SaCheckPermission("oce:titleWrap:add")
    @Log(title = "标题包", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/createWrapTitle")
    public R<Void> createWrapTitle(@Validated(AddGroup.class) @RequestBody OceWrapTitleBo bo) {
        return toAjax(oceTitleWrapService.insertWrapTitle(bo));
    }


    /**
     * 修改标题包下-标题
     */
    @SaCheckPermission("oce:titleWrap:edit")
    @Log(title = "标题包", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/updateWrapTitle")
    public R<Void> updateWrapTitle(@Validated(EditGroup.class) @RequestBody OceWrapTitleBo bo) {
        return toAjax(oceTitleWrapService.updateWrapTitle(bo));
    }


    /**
     * 删除标题包下-标题
     */
    @SaCheckPermission("oce:titleWrap:edit")
    @Log(title = "标题包", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/deleteWrapTitle")
    public R<Void> deleteWrapTitle(@Validated(EditGroup.class) @RequestBody OceWrapTitleBo bo) {
        return toAjax(oceTitleWrapService.deleteWrapTitle(bo));
    }

    /**
     * 修改标题包
     */
    @SaCheckPermission("oce:titleWrap:edit")
    @Log(title = "标题包", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody OceTitleWrapBo bo) {
        return toAjax(oceTitleWrapService.updateByBo(bo));
    }

    /**
     * 删除标题包
     *
     * @param ids 主键串
     */
    @SaCheckPermission("oce:titleWrap:remove")
    @Log(title = "标题包", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(oceTitleWrapService.deleteWithValidByIds(List.of(ids), true));
    }
}
